package com.example.demo.mapper;

import com.example.demo.entity.Post;
import com.example.demo.entity.PostVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PostMapper {

    // 插入帖子
    @Insert("INSERT INTO post (user_id, title, content) VALUES (#{userId}, #{title}, #{content})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertPost(Post post);

    // 插入帖子标签关联
    @Insert({
            "<script>",
            "INSERT INTO post_tag_relation (post_id, tag_id) VALUES ",
            "<foreach collection='tagIds' item='tagId' separator=','>",
            "  (#{postId}, #{tagId})",
            "</foreach>",
            "</script>"
    })
    void insertPostTags(@Param("postId") Long postId, @Param("tagIds") List<Long> tagIds);

    // 查询帖子列表
    @Select({
            "<script>",
            "SELECT DISTINCT p.id, p.user_id AS userId, u.avatar AS userAvatar, u.username, p.title, p.content, p.created_at AS createdAt, p.updated_at AS updatedAt ",
            "FROM post p ",
            "JOIN user u ON p.user_id = u.id ",
            "LEFT JOIN post_tag_relation ptr ON p.id = ptr.post_id ",
            "LEFT JOIN tag t ON ptr.tag_id = t.id ",
            "<where>",
            "  <if test='keyword != null and keyword != \"\"'>",
            "    AND (p.title LIKE CONCAT('%', #{keyword}, '%') ",
            "    OR p.content LIKE CONCAT('%', #{keyword}, '%') ",
            "    OR u.username LIKE CONCAT('%', #{keyword}, '%') ",
            "    OR t.name LIKE CONCAT('%', #{keyword}, '%'))",
            "  </if>",
            "  <if test='tagName != null and tagName != \"\"'>",
            "      AND t.name LIKE CONCAT('%', #{tagName}, '%')",
            "  </if>",
            "  <if test='startTime != null and endTime != null'>",
            "    AND DATE(p.created_at) BETWEEN #{startTime} AND #{endTime}",
            "  </if>",
            "</where>",
            "LIMIT #{limit} OFFSET #{offset}",
            "</script>"
    })
    List<PostVO> selectPostsWithUserInfoByKeyword(
            @Param("keyword") String keyword,
            @Param("tagName") String tagName,
            @Param("startTime") String startTime,
            @Param("endTime") String endTime,
            @Param("limit") int limit,
            @Param("offset") int offset
    );

    @Select({
            "<script>",
            "SELECT COUNT(DISTINCT p.id) ",
            "FROM post p ",
            "JOIN user u ON p.user_id = u.id ",
            "LEFT JOIN post_tag_relation ptr ON p.id = ptr.post_id ",
            "LEFT JOIN tag t ON ptr.tag_id = t.id ",
            "<where>",
            "  <if test='keyword != null and keyword != \"\"'>",
            "    AND (p.title LIKE CONCAT('%', #{keyword}, '%') ",
            "    OR p.content LIKE CONCAT('%', #{keyword}, '%') ",
            "    OR u.username LIKE CONCAT('%', #{keyword}, '%') ",
            "    OR t.name LIKE CONCAT('%', #{keyword}, '%'))",
            "  </if>",
            "  <if test='tagName != null and tagName != \"\"'>",
            "   AND t.name LIKE CONCAT('%', #{tagName}, '%')",
            "  </if>",
            "  <if test='startTime != null and endTime != null'>",
            "    AND DATE(p.created_at) BETWEEN #{startTime} AND #{endTime}",
            "  </if>",
            "</where>",
            "</script>"
    })
    int countPostsByKeyword(
            @Param("keyword") String keyword,
            @Param("tagName") String tagName,
            @Param("startTime") String startTime,
            @Param("endTime") String endTime
    );


    // 查询帖子标签
    @Select("SELECT t.name FROM tag t JOIN post_tag_relation ptr ON t.id = ptr.tag_id WHERE ptr.post_id = #{postId}")
    List<String> selectTagsByPostId(@Param("postId") Long postId);
    //根据帖子id获取帖子内容
    @Select({
            "SELECT p.id, p.user_id AS userId, u.avatar AS userAvatar, u.username, p.title, p.content, p.created_at AS createdAt, p.updated_at AS updatedAt ",
            "FROM post p ",
            "JOIN user u ON p.user_id = u.id ",
            "WHERE p.id = #{postId}"
    })
    PostVO selectPostById(@Param("postId") Long postId);

    // 根据用户ID或用户名查询帖子
    @Select({
            "<script>",
            "SELECT p.id, p.user_id AS userId, u.avatar AS userAvatar, u.username, p.title, p.content, p.created_at AS createdAt, p.updated_at AS updatedAt ",
            "FROM post p ",
            "JOIN user u ON p.user_id = u.id ",
            "<where>",
            "  <if test='userId != null'>",
            "    AND u.id = #{userId}",
            "  </if>",
            "  <if test='username != null and username != \"\"'>",
            "    AND u.username LIKE CONCAT('%', #{username}, '%')",
            "  </if>",
            "</where>",
            "ORDER BY p.created_at DESC ",
            "LIMIT #{limit} OFFSET #{offset}",
            "</script>"
    })
    List<PostVO> selectPostsByUser(
            @Param("userId") Long userId,
            @Param("username") String username,
            @Param("limit") int limit,
            @Param("offset") int offset
    );

    // 根据用户ID或用户名查询帖子总数
    @Select({
            "<script>",
            "SELECT COUNT(p.id) ",
            "FROM post p ",
            "JOIN user u ON p.user_id = u.id ",
            "<where>",
            "  <if test='userId != null'>",
            "    AND u.id = #{userId}",
            "  </if>",
            "  <if test='username != null and username != \"\"'>",
            "    AND u.username LIKE CONCAT('%', #{username}, '%')",
            "  </if>",
            "</where>",
            "</script>"
    })
    int countPostsByUser(
            @Param("userId") Long userId,
            @Param("username") String username
    );
}
